#!/bin/bash

home_path="../"
results_dir="SimResults"

runs=$1
p_id=$2
id=$3
noise=$4
nodes=$5

seeds_dir="seeds/"

seed=`cat $seeds_dir"runsim_seeds.txt" | grep "$nodes\-$noise\-$id " | cut -d' ' -f2`
echo $seed

for ((r=1 ; $r<=$runs ; r++)); do
	python runsim.py --noise_model=$noise --nodes_number=$nodes --topology_id=$id --seed=$seed > outputlog.log 							# runs the simulation with the specidic number of nodes, noise model and id.
	wait
	cp sim_topology.mat "sim_topology-$nodes-$noise-$id.mat"
	wait
	tar cvfz outputlog.tar.gz outputlog.log
	wait
	tar cvfz "results-$nodes-$noise-$id.tar.gz" outputlog.tar.gz seed.txt sim_topology.mat stats_*.mat runsim.py network_stat_re.mat network_stat_fe.mat	# zip the files in the results file
	wait
	rm -f outputlog.log stats_*.mat sim_topology.mat network_stat_* seed.txt													# removes output files																									
	mv "results-$nodes-$noise-$id.tar.gz" "$home_path""$results_dir""/$noise""-$nodes"										# moves the zipped file in the results directory
	mv "sim_topology-$nodes-$noise-$id.mat" "$home_path""$results_dir""/$noise""-$nodes"
	wait
	id=$(($id+1))
																										# increments id number
done

remove=`pwd`"/CleanTempDir.sh $p_id"

bash $remove
exit

#python runsim.py > outputlog.log
#tar cvfz results.tar.gz outputlog.log stats_*.mat topology.out linkgain.out network_config.txt runsim.py meyer-heavy.txt sim_topology.mat
#rm -f outputlog.log stats_*.mat topology.out linkgain.out network_config.txt runsim.py meyer-heavy.txt sim_topology.mat

#cp -r ../RuinsMonitor-2.0.0 ../Temp1-RuinsMonitor-2.0.0
#cd ../Temp1-RuinsMonitor-2.0.0
#make clean
#make micaz sim
#cd ../RuinsMonitor-2.0.0
